//
//  ZHFRadioHeaderView.m
//  GoTraveling
//
//  Created by dllo on 15/12/26.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "ZHFRadioHeaderView.h"

#import "ZHFHotList.h"

@interface ZHFRadioHeaderView ()<UIScrollViewDelegate>

@property (nonatomic, retain)UIScrollView *headScrollV;
@property (nonatomic, retain)UIPageControl *headPage;
@property (nonatomic, retain)UIButton *moringButton;
@property (nonatomic, retain)UIButton *sevenButton;
@property (nonatomic, retain)UIButton *newsButton;
@property (nonatomic, retain)ZHFHotList *hotList;
@property (nonatomic, retain)NSMutableArray *imageArray;
@property (nonatomic, retain)NSMutableArray *urlArray;

@end

@implementation ZHFRadioHeaderView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews{
    
    self.backgroundColor = [UIColor whiteColor];
    
    UIView *nilView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 1, SCREEN_WIDTH, 1)];
    nilView.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1];
    [self addSubview:nilView];
    
    self.headScrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 180 * ADAPT_HEIGHT)];
    self.headScrollV.backgroundColor = [UIColor grayColor];
    [self addSubview:self.headScrollV];
    
    self.imageArray = [NSMutableArray array];
    self.urlArray = [NSMutableArray array];
    
    self.headScrollV.delegate = self;
    // 按页滑动
    self.headScrollV.pagingEnabled = YES;
    // 取消边界反弹效果
    self.headScrollV.bounces = NO;
    // 是否显示水平滑动条
    self.headScrollV.showsHorizontalScrollIndicator = NO;
    // 初始偏移量
    self.headScrollV.contentOffset = CGPointMake(self.bounds.size.width, 0);
    // 设置页码
    self.headPage = [[UIPageControl alloc] initWithFrame:CGRectMake(self.headScrollV.frame.size.width / 1.6, self.headScrollV.frame.size.height - self.headScrollV.frame.size.height / 5, self.headScrollV.frame.size.width / 2, self.headScrollV.frame.size.height / 6)];
    self.headPage.backgroundColor = [UIColor clearColor];
    [self addSubview:self.headPage];
    // 页码个数
    self.headPage.numberOfPages = 3;
    // 从第几个开始显示
    self.headPage.currentPage = 0;

#pragma mark- 设置定时器
    // 参数1.时间
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(changePhoto:) userInfo:@"time" repeats:YES];
    
    self.newsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.newsButton.frame = CGRectMake(self.frame.origin.x + 10 * ADAPT_WIDTH, self.headScrollV.frame.size.height + 7 * ADAPT_HEIGHT, (SCREEN_WIDTH - 30 * ADAPT_WIDTH) / 3, (SCREEN_WIDTH - 30 * ADAPT_HEIGHT) / 3);
//    self.newsButton.backgroundColor = [UIColor grayColor];
    [self addSubview:self.newsButton];
    self.newsButton.tag = 1000;
    [self.newsButton addTarget:self action:@selector(newsButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.moringButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.moringButton.frame = CGRectMake(self.newsButton.frame.origin.x + self.newsButton.frame.size.width + 5 * ADAPT_WIDTH, self.headScrollV.frame.size.height + 7 * ADAPT_HEIGHT, (SCREEN_WIDTH - 30 * ADAPT_WIDTH) / 3, (SCREEN_WIDTH - 30 * ADAPT_HEIGHT) / 3);
//    self.moringButton.backgroundColor = [UIColor grayColor];
    [self addSubview:self.moringButton];
    self.moringButton.tag = 1001;
    [self.moringButton addTarget:self action:@selector(newsButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.sevenButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.sevenButton.frame = CGRectMake(self.moringButton.frame.origin.x + self.moringButton.frame.size.width + 5 * ADAPT_WIDTH, self.headScrollV.frame.size.height + 7 * ADAPT_HEIGHT, (SCREEN_WIDTH - 30 * ADAPT_WIDTH) / 3, (SCREEN_WIDTH - 30 * ADAPT_HEIGHT) / 3);
//    self.sevenButton.backgroundColor = [UIColor grayColor];
    [self addSubview:self.sevenButton];
    self.sevenButton.tag = 1002;
    [self.sevenButton addTarget:self action:@selector(newsButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark- hotList赋值
- (void)presentToHotListArray:(NSMutableArray *)array{
    NSMutableArray *ImgArray = [NSMutableArray array];
    for (NSMutableDictionary *dic in array) {
        [ImgArray addObject:[dic objectForKey:@"coverimg"]];
    }
    for (int i = 0; i < ImgArray.count; i++) {
        NSURL *urlImage = [NSURL URLWithString:[ImgArray objectAtIndex:i]];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.newsButton.bounds];
        if (i == 0) {
            [imageView sd_setImageWithURL:urlImage placeholderImage:[UIImage imageNamed:@"cm2_default_cover_240.png"]];
            [self.newsButton addSubview:imageView];
        }
        if (i == 1) {
            [imageView sd_setImageWithURL:urlImage placeholderImage:[UIImage imageNamed:@"cm2_default_cover_240.png"]];
            [self.moringButton addSubview:imageView];
        }
        if (i == 2) {
            [imageView sd_setImageWithURL:urlImage placeholderImage:[UIImage imageNamed:@"cm2_default_cover_240.png"]];
            [self.sevenButton addSubview:imageView];
        }
    }
}

#pragma mark- 轮播图赋值
- (void)presentToCarouselArray:(NSMutableArray *)array{
    self.headPage.numberOfPages = array.count;
    NSMutableArray *tempArray = [NSMutableArray array];
    for (NSMutableDictionary *dic in array) {
        [tempArray addObject:[dic objectForKey:@"img"]];
        [self.urlArray addObject:[dic objectForKey:@"url"]];
    }
    [self.imageArray addObject:tempArray.lastObject];
    [self.imageArray addObjectsFromArray:tempArray];
    [self.imageArray addObject:tempArray.firstObject];
    self.headScrollV.contentSize = CGSizeMake(self.bounds.size.width * self.imageArray.count, 180);
    for (int i = 0; i < self.imageArray.count; i++) {
        NSURL *urlImg = [NSURL URLWithString:[self.imageArray objectAtIndex:i]];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, 180 * ADAPT_HEIGHT)];
        [imageView sd_setImageWithURL:urlImg placeholderImage:[UIImage imageNamed:@"trip_edit_empty_content@2x.png"]];
        [self.headScrollV addSubview:imageView];
        self.headScrollV.delegate = self;
    }
}


- (void)newsButtonAction:(UIButton *)btn{
    
}

#pragma mark- 减速停止时触发
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (self.headScrollV.contentOffset.x == SCREEN_WIDTH * (self.imageArray.count - 1)) {
        scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    } else if (self.headScrollV.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * (self.imageArray.count - 2), 0);
    }
    
    self.headPage.currentPage = (scrollView.contentOffset.x - SCREEN_WIDTH) / SCREEN_WIDTH;
    
}
#pragma mark- 定时器方法实现
-(void)changePhoto:(NSTimer *)time{
    // 记录开始的偏移量
    [UIView animateWithDuration:0.5 animations:^{
        
        CGPoint tempContentSize = self.headScrollV.contentOffset;
        tempContentSize.x += SCREEN_WIDTH;
        self.headScrollV.contentOffset = tempContentSize;
        
        UIScrollView *tempSV = [[UIScrollView alloc] init];
        tempSV.contentOffset = self.headScrollV.contentOffset;
    }];
    if(self.headScrollV.contentOffset.x == (self.imageArray.count - 1) * SCREEN_WIDTH){
        self.headScrollV.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    } else if(self.headScrollV.contentOffset.x == 0){
        self.headScrollV.contentOffset = CGPointMake(SCREEN_WIDTH * (self.imageArray.count - 2), 0);
    }
    self.headPage.currentPage  = (self.headScrollV.contentOffset.x - SCREEN_WIDTH) / SCREEN_WIDTH ;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
